﻿using OpenIddict.Abstractions;
using System.Globalization;
using Volo.Abp;
using Volo.Abp.DependencyInjection;

namespace Ebusiness_Authservice.Services
{
    /// <summary>
    /// 
    /// </summary>
    [Dependency(ServiceLifetime.Singleton)]
    public class ScopesService : IScopesService
    {

        //public IOpenIddictScopeManager _ScopeManager { get; set; }

        public IOpenIddictScopeManager openIddictScopeManager { get; set; }

        public ScopesService()
        {

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="Name"></param>
        /// <param name="DisplayName"></param>
        /// <returns></returns>
        public async Task CreatAsync(string Name, string DisplayName)
        {
            #region abp opendidct框架
            {
                //// 微服务信息保存
                //if (await _ScopeManager.FindByNameAsync(Name) == null)
                //{
                //    await _ScopeManager.CreateAsync(new OpenIddictScopeDescriptor()
                //    {
                //        Name = Name, // 微服务名称
                //        DisplayName = DisplayName,
                //        DisplayNames =
                //    {
                //        [CultureInfo.GetCultureInfo("zh-Hans")] = "演示 API 访问",
                //        [CultureInfo.GetCultureInfo("tr")] = "API erişimi"
                //    },
                //        Resources =
                //    {
                //        "select", // 微服务名称接口,直接写接口名称
                //        "delete",
                //        "update",
                //        "insert"
                //    }
                //    });
                //}
            }
            #endregion

            #region 原生框架 openddict
            {
                // 创建API作用域
                if (await openIddictScopeManager.FindByNameAsync(Name) == null)
                {
                    await openIddictScopeManager.CreateAsync(new OpenIddictScopeDescriptor
                    {
                        Name = Name,                    // 作用域名称
                        DisplayName = DisplayName,        // 显示名称
                        DisplayNames =
                            {
                                [CultureInfo.GetCultureInfo("zh-Hans")] = "演示 API 访问",
                                [CultureInfo.GetCultureInfo("tr")] = "API erişimi"
                            },
                        Resources = { "OrderService_resource_server" }  // 资源服务器标识
                    });
                }
            }
            #endregion
        }
    }
}
